const { ordersModel } = require('./models/ordersModels');
const { customersModel } = require('./models/customersModels');
module.exports.addOrders = async function (params) {
    return await ordersModel.create(params)
}


module.exports.getOrders = async params => {
    const allTotal = await ordersModel.countDocuments({
        delete: false,
        [params.type]: { $regex: params.value, $options: '$i' }
    });

    const toCancelTotal = await ordersModel.countDocuments({
        delete: false,
        [params.type]: { $regex: params.value, $options: '$i' },
        $or: [
            {
                ordersPayState: 2,
            }
            ,
            {
                ordersPayState: 3
            }
        ]
    });
    // const pages = Math.ceil(total / params.pageSize);
    const data = await ordersModel.aggregate([
        {
            // 条件查询
            $match: {
                delete: false,
                [params.type]: { $regex: params.value, $options: '$i' }
            }
        },
        {
            $skip: params.pageSize * (params.currentPage - 1)
        },
        {
            $limit: params.pageSize * 1
        }
    ])

    const information = await ordersModel.populate(data, {
        path: 'insuresId'
    })
    const orders = await ordersModel.populate(information, {
        path: 'customersId'
    })

    const data1 = await ordersModel.aggregate([
        {
            // 条件查询
            $match: {
                delete: false,
                [params.type]: { $regex: params.value, $options: '$i' },
                $or: [
                    {
                        ordersPayState: 2,
                    }
                    ,
                    {
                        ordersPayState: 3
                    }
                ]
            }
        },
        {
            $skip: params.pageSize * (params.currentPage - 1)
        },
        {
            $limit: params.pageSize * 1
        }
    ])

    const information1 = await ordersModel.populate(data1, {
        path: 'insuresId'
    })
    const toCancelOrders = await ordersModel.populate(information1, {
        path: 'customersId'
    })

    return {
        orders,
        allTotal,
        toCancelTotal,
        toCancelOrders
    }
}

module.exports.getToCancelOrders = async params => {
    console.log(params);
    const total = await ordersModel.getToCancelOrders({
        delete: false,
        [params.type]: { $regex: params.value, $options: '$i' },
        $or: [
            {
                ordersPayState: 2
            },
            {
                ordersPayState: 3
            }
        ]
    });
    const data = await ordersModel.aggregate([
        {
            // 条件查询
            $match: {
                $match: {
                    delete: false,
                    [params.type]: {
                        $regex: value,
                        $options: "$i"
                    },
                    $or: [
                        {
                            ordersPayState: 2
                        },
                        {
                            ordersPayState: 3
                        }
                    ]
                }
            }
        },
        {
            $skip: params.pageSize * (params.currentPage - 1)
        },
        {
            $limit: params.pageSize * 1
        }
    ]);

    const information = await ordersModel.populate(data, {
        path: 'insuresId'
    })
    const orders = await ordersModel.populate(information, {
        path: 'customersId'
    })
    return {
        orders,
        total,
    }
}

module.exports.getOrdersById = async function (params) {
    return await ordersModel.find({ _id: params._id, delete: false })
}

module.exports.disposeOrdersToCancel = async function (params) {
    return await ordersModel.updateOne({ _id: params._id, delete: false }, { ordersPayState: 3 })
}

module.exports.submitOrdersToCancel = async function (params) {
    console.log(params);
    return await ordersModel.updateOne({ _id: params._id, delete: false }, { ordersPayState: 2 })
}


module.exports.changeOrderById = async function (params) {
    console.log(params);
    const data = await ordersModel.updateOne({ _id: params._id, delete: false }, { ...params })
    // const data1 = await usersModel.updateOne({ _id: params._id, delete: false }, { ordersPayState: 2 })
    return (data)

}


